import XEUtils from "xe-utils";
import VXETable from "vxe-table";
import common from './common';
import CustomSwitch from "../components/CustomSwitch.vue";

VXETable.renderer.add("CustomSwitch", {
    autofocus: "input.el-input__inner",
    renderHeader(h, renderOpts, params) {
        const { column } = params;
        const { header } = renderOpts;
        if (typeof header === 'function') {
            return [header(h, params)];
        }
        return column.title;
    },
    renderDefault(h, renderOpts, params) {
        const { row, column } = params;
        const { attrs, props } = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        return [
            h(CustomSwitch, {
                attrs: attrs,
                props: $.extend({}, props, { params, value: cellValue }),
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    },
    renderEdit(h, renderOpts, params) {
        const { row, column } = params;
        const { attrs, props } = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        return [
            h(CustomSwitch, {
                attrs: attrs,
                props: $.extend({}, props, { params, value: cellValue }),
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    },
    // 可编辑显示模板
    renderCell (h, renderOpts, params) {
        const { row, column } = params;
        const { attrs, props } = renderOpts;
        let cellValue = XEUtils.get(row, column.property);
        return [
            h(CustomSwitch, {
                attrs: attrs,
                props: $.extend({}, props, { params, value: cellValue }),
                on: common.getEditOns(renderOpts, params),
                nativeOn: common.getNativeOns(renderOpts, params)
            })
        ];
    }
});
